package servlet;

import dao.ProductDAO;
import entity.PageBean;
import entity.Product;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "productPage",value = "/product/page")
public class ProductPageServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1. 接受分页参数pageNum pageSize;
        String num = req.getParameter("pageNum");
        String size = req.getParameter("pageSize");
        int pageNum = Integer.parseInt(num);
        int pageSize = Integer.parseInt(size);

        //2. 转化skip和limit
        int skip = (pageNum-1)*pageSize;
        int limit = pageSize;
        //3. list = dao.findPage(skip,limit)
        ProductDAO dao = new ProductDAO();
        List<Product> list = dao.findPage(skip, limit);
        long total = dao.getCount();
        //4. new PageBean(list,total,num,size);
        PageBean<Product> page = new PageBean<Product>();
        page.setTotal(total);
        page.setPageSize(pageSize);
        page.setPageNum(pageNum);
        page.setList(list);
        page.setPageTotal(total,pageSize);
        //5. 讲page传给List.jsp 跳转。
        req.setAttribute("page",page);
        req.getRequestDispatcher("/product/List.jsp").forward(req,resp);
    }
}
